//
//  ViewController.m
//  TableViewSquaredDemo
//
//  Created by Rannie on 13-9-10.
//  Copyright (c) 2013年 Rannie. All rights reserved.
//

#import "ViewController.h"
#import "ProductCell.h"
#import "Product.h"

@interface ViewController () <ProductCellDelegate>
{
    NSMutableArray *_productList;
}

@end

static NSString * CellIdentifier = @"ProductCell";
@implementation ViewController

#pragma mark -
#pragma mark View Lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    //self.title = @"宝贝商品";
    [self.navigationController setNavigationBarHidden:YES];
    //self.tableView.backgroundColor = [UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1.0];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.allowsSelection = NO;
    [self.tableView registerClass:[ProductCell class] forCellReuseIdentifier:CellIdentifier];
    [self loadData];
}

#pragma mark - 
#pragma mark LoadData

- (void)loadData
{
    static NSString * const TitleKey = @"title";
    static NSString * const ImageNameKey = @"imagename";
    static NSString * const FlagKey = @"flag";
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Products" ofType:@"plist"];
    NSArray *array = [NSArray arrayWithContentsOfFile:path];
    
    if (!array)
    {
        //MyLog(@"文件加载失败");
    }
    _productList = [NSMutableArray arrayWithCapacity:array.count];
    [array enumerateObjectsUsingBlock:^(NSDictionary *dict, NSUInteger idx, BOOL *stop) {
        Product *product = [[Product alloc] init];
        product.title = dict[TitleKey];
        product.imageName = dict[ImageNameKey];
        product.flag = [dict[FlagKey] integerValue];
        [_productList addObject:product];
    }];
}

#pragma mark -
#pragma mark TableView DataSource And Delegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger number = (_productList.count - 1)/RColCount + 1;
    //MyLog(@"%d", number);
    return number;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ProductCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    NSInteger index = indexPath.row;
    NSMutableArray *list = [NSMutableArray arrayWithCapacity:RColCount];
    for (NSInteger i = 0; i < RColCount; i++)
    {
        [list addObject:_productList[index*RColCount + i]];
    }
    
    cell.cellDelegate = self;
    [cell bindProducts:list];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return RCellHeight;
}

#pragma mark - 
#pragma mark TapButton Action

- (void)productCell:(ProductCell *)cell actionWithFlag:(NSInteger)flag
{
    //MyLog(@"flag:%d", flag);
    //点击不同的按钮进行不同的处理
//    switch (flag)
//    {
//        case <#constant#>:
//            <#statements#>
//            break;
//            
//        default:
//            break;
//    }
}

@end
